package constructionScheme;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;

public class ConstructionSchemeAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	ConstructionSchemeDao constructionSchemeDao;
	
	ConstructionScheme constructionScheme;
	
	private File upload;
	
	private String uploadContentType;
	
	private String uploadFileName;
	
	private InputStream inputStream;
	
	private Photo photo;
	
	private PhotoDao photoDao;
	
	private List<ConstructionScheme> contructionSchemeList = new ArrayList<ConstructionScheme>();

	private Long photoId;
	
	public Long getPhotoId() {
		return photoId;
	}

	public void setPhotoId(Long photoId) {
		this.photoId = photoId;
	}

	public List<ConstructionScheme> getContructionSchemeList() {
		return contructionSchemeList;
	}

	public void setContructionSchemeList(
			List<ConstructionScheme> contructionSchemeList) {
		this.contructionSchemeList = contructionSchemeList;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public PhotoDao getPhotoDao() {
		return photoDao;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public ConstructionScheme getConstructionScheme() {
		return constructionScheme;
	}

	public void setConstructionScheme(ConstructionScheme constructionScheme) {
		this.constructionScheme = constructionScheme;
	}

	public ConstructionSchemeDao getConstructionSchemeDao() {
		return constructionSchemeDao;
	}

	public void setConstructionSchemeDao(ConstructionSchemeDao constructionSchemeDao) {
		this.constructionSchemeDao = constructionSchemeDao;
	}
	public String intoConstructionScheme(){
		constructionScheme = constructionSchemeDao.findById(1L);
		return SUCCESS;
	}
	@Transactional(propagation=Propagation.REQUIRED)
	public String addConstructionScheme()throws Exception{
		
		if(this.upload!=null&&this.constructionScheme.getContent()!=null){
			this.photo = new Photo();
			this.inputStream = new FileInputStream(this.getUpload());
			this.photo.setPicData(new byte[(int)this.getUpload().length()]);
			inputStream.read(this.photo.getPicData());
			inputStream.close();
			System.out.println("<<<<<<<<<<<<<<<<"+this.constructionSchemeDao.findAll());
			if(!this.constructionSchemeDao.findAll().toString().equals("[]")){
				System.out.println("这是判断数据库不为空的");
//					this.photo.setPicId(1L);
//					this.constructionScheme.setId(1L);
				this.photoDao.saveOrUpdate(this.photo);
				this.constructionScheme.setPhoto(this.photo);
				this.constructionSchemeDao.saveOrUpdate(this.constructionScheme);
				return INPUT;
			}else{
				System.out.println("这是数据库里为空的");
				this.photoDao.create(photo);
				this.constructionScheme.setPhoto(photo);
				this.constructionSchemeDao.create(constructionScheme);
				return SUCCESS;
			}
		}else{
			if(constructionSchemeDao.findById(constructionScheme.getId()).getPhoto()!=null){
				System.out.println("进入了没上传图片，但自身有图片 的方法");
				constructionScheme.setPhoto(photoDao.findById(constructionSchemeDao.findById(constructionScheme.getId()).getPhoto().getPicId()));
				this.constructionSchemeDao.saveOrUpdate(this.constructionScheme);
				return SUCCESS;
			}
			System.out.println("进入了没有上传图片，但自身没图片 的方法");
			this.constructionSchemeDao.saveOrUpdate(this.constructionScheme);
			return SUCCESS;
		}
		
	}
	public String showConstructionScheme(){
		if(!this.constructionSchemeDao.findAll().toString().equals("[]")){
			this.contructionSchemeList = this.constructionSchemeDao.findAll();
			this.constructionScheme = this.contructionSchemeList.get(this.contructionSchemeList.size()-1);
		}
		return SUCCESS;
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public String getPhotoById(){
		System.out.println("图片999的id是："+this.constructionScheme.getPhoto().getPicId());
		this.getInputStream();
		return SUCCESS;
	}
	public InputStream getInputStream() {
		System.out.println("图片1的id是："+this.constructionScheme.getPhoto().getPicId());
		this.photo = this.photoDao.findById(this.constructionScheme.getPhoto().getPicId());
		return new ByteArrayInputStream(this.photo.getPicData());
	}

}
